/* Project name:              ADC
 
 * Board:                     MINI-M0

 * MCU:                       STM32F051R8
 
 * IDE:                       mikroC PRO for ARM
     
 * Copyright:
     (c) Anton Kovalev        https://www.gotomain.ru/
     (c) STMicroelectronics   https://www.st.com/
     (c) Mikroelektronika     https://www.mikroe.com/
*/

unsigned int adc_value;
float analog_value;
char analog_value_string[15];

// UART write text and new line (carriage return + line feed)
void UART_Write_Line(char *uart_text) {
  UART_Write_Text(uart_text);
  UART_Write(13);
  UART_Write(10);
}

void main() {

  ADC_Set_Input_Channel(_ADC_CHANNEL_0);    // Choose ADC channel
  ADC1_Init();                              // Init ADC module
  Delay_ms(100);                            // Wait for ADC module to stabilize

  UART2_Init(115200);                       // Initialize UART module at 56000 bps
  Delay_ms(100);                            // Wait for UART module to stabilize


  while (1) {
    adc_value = ADC1_Get_Sample(0);                // Get ADC value from corresponding channel
    analog_value = (adc_value * 3.30) / 4095.0;    // Calulate applied analog voltage
    FloatToStr(analog_value, analog_value_string); // Convert analog value to string
    Ltrim(analog_value_string);                    // Clear leading spaces of the string
    UART_Write_Line(analog_value_string);
    Delay_ms(50);
  }
}